package com.fare.taluo.core.domain.config.product.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.config.product.ProductEntity;
import com.fare.taluo.core.domain.config.product.ProductEntityRepository;
import com.fare.taluo.util.StringUtils;
@Repository
public class ProductEntityRepositoryImpl extends EntityRepositoryHibernate
	implements ProductEntityRepository{

	@Override
	public List<ProductEntity> findAll() {
		return super.findAll(ProductEntity.class);
	}

	@Override
	public List<ProductEntity> findProductByConditions(String name,String companyName,
			String startCity,String endCity,String productType) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from ProductEntity p where 1=1" ;
		if(StringUtils.isNotEmpty(name)){
			hql += " and p.name= ?";
			params.add(name);
		}
		if(StringUtils.isNotEmpty(companyName)){
			hql += " and p.companyName= ?";
			params.add(companyName);
		}
		if(StringUtils.isNotEmpty(startCity)){
			hql += " and p.startCity= ?";
			params.add(startCity);
		}
		if(StringUtils.isNotEmpty(endCity)){
			hql += " and p.endCity= ?";
			params.add(endCity);
		}
		if(StringUtils.isNotEmpty(productType)){
			hql += " and p.productType= ?";
			params.add(productType);
		}
		List<ProductEntity> list = super.find(hql,params.toArray(),ProductEntity.class);
		return list;
	}
	
	@Override
	public void saveOrUpdate(ProductEntity productEntity) {
		super.save(productEntity);
	}

	@Override
	public void delete(ProductEntity productEntity) {
		super.remove(productEntity);
	}


	

}
